import re
from rest_framework import serializers
from api.models.product import Porduct, PorductImage
from api.models.stock_record import StockPrice, StockRecord
from api.utils.backends import BucketOperation
from api.utils.enums import PriceType, MediaType
from api.utils.serializers import PaginationSerializer, PaginationResultSerializer


class StockPriceBodySerializer(serializers.ModelSerializer):
    class Meta:
        model = StockPrice
        fields = ["type", "price"]


class StockRecordBodySerializer(serializers.ModelSerializer):
    price = StockPriceBodySerializer(many=True)

    class Meta:
        model = StockRecord
        fields = ["num_in_stock", "num_allocated", "price"]


class FileOfProduct(serializers.ModelSerializer):
    bucket_operation = BucketOperation()
    thumbnail_image = serializers.SerializerMethodField(help_text="缩略图", required=False)

    class Meta:
        model = PorductImage
        fields = ("id", "purpose", "type", "file", "thumbnail_image", "created_at")

    def get_thumbnail_image(self, file_info):
        thumbnail_image = None
        if file_info.type == MediaType.IMAGE.value:
            thumbnail_image = self.bucket_operation.url(
                file_info.file.name,
                params={
                    "x-oss-process": "image/auto-orient,1/resize,m_lfit,w_750,h_600/quality,q_90"
                },
            )
        return thumbnail_image


class PorductDataSerializer(PaginationSerializer, serializers.ModelSerializer):
    stock_record = StockRecordBodySerializer(many=True)
    images = FileOfProduct(many=True)
    videos = FileOfProduct(many=True)

    class Meta:
        model = Porduct
        fields = [
            "id",
            "product_id",
            "title",
            "businss_id",
            "description",
            "is_public",
            "stock_record",
            "images",
            "videos",
        ]


class ProductPutSerializer(serializers.ModelSerializer):
    class Meta:
        model = Porduct
        fields = ["title", "description", "is_public"]


class ProductBodySerializer(PaginationResultSerializer):
    data = PorductDataSerializer(many=True)


class ProductIdSerializer(serializers.ModelSerializer):
    class Meta:
        model = Porduct
        fields = ["product_id"]


class ProductQuerySerializer(PaginationSerializer, serializers.ModelSerializer):
    class Meta:
        model = Porduct
        fields = [
            "per_page",
            "page",
            "limit",
            "product_id",
            "title",
            "businss_id",
            "is_public",
        ]
        extra_kwargs = {"title": {"required": False}}


class ProductImageBodySerializer(serializers.ModelSerializer):
    class Meta:
        model = PorductImage
        fields = ["id", "file", "type", "purpose"]


class ProductImageUpdateSerializer(serializers.ModelSerializer):
    class Meta:
        model = PorductImage
        fields = ("purpose", "type", "file")
        extra_kwargs = {
            "purpose": {"required": True},
            "media_type": {"required": True},
            "file": {"required": True},
        }


class AdminProductQuerySerializer(serializers.ModelSerializer):
    num_in_stock = serializers.IntegerField(help_text="库存数量", default=0)
    pay_type = serializers.ChoiceField(help_text="支付类型", choices=PriceType.to_choices())
    price = serializers.IntegerField(help_text="价格", default=0)

    class Meta:
        model = Porduct
        fields = [
            "product_id",
            "title",
            "businss_id",
            "description",
            "is_public",
            "num_in_stock",
            "pay_type",
            "price",
        ]
        extra_kwargs = {
            "title": {"required": False},
            "product_id": {"required": True},
            "businss_id": {"required": True},
            "is_public": {"required": False},
        }


class AdminProductImageQeurySerializer(serializers.ModelSerializer):
    product_id = serializers.IntegerField(help_text="商品ID", default=0)

    class Meta:
        model = PorductImage
        fields = [
            "purpose",
            "file",
            "type",
            "product_id",
        ]
        extra_kwargs = {
            "file": {"required": True},
            "product_id": {"required": True},
            "type": {"required": True},
            "purpose": {"required": True},
        }


class AdminProductImageIDSerializer(serializers.ModelSerializer):
    class Meta:
        model = PorductImage
        fields = ["id"]
